package com.github.catvod.spider;

import android.text.TextUtils;

import com.github.catvod.bean.Class;
import com.github.catvod.bean.Result;
import com.github.catvod.bean.Vod;
import com.github.catvod.crawler.Spider;
import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * APP影院
 * @author fs
 */
public class AppYy extends Spider {
    private String sisterUrl="https://www.appmovie.vip/";

    private Map<String, String> getHeader() {
        Map<String, String> headers = new HashMap<>();
        headers.put("User-Agent", Util.CHROME);
        return headers;
    }

    @Override
    public String homeContent(boolean filter) throws Exception {
        List<com.github.catvod.bean.Class> classes = new ArrayList<>();
        List<String> typeIds = Arrays.asList("1", "2", "3", "4");
        List<String> typeNames = Arrays.asList("电影", "电视剧", "综艺", "动漫");
        for (int i = 0; i < typeIds.size(); i++) classes.add(new Class(typeIds.get(i), typeNames.get(i)));
        return Result.string(classes, new ArrayList<>());
    }

    @Override
    public String categoryContent(String tid, String pg, boolean filter, HashMap<String, String> extend) throws Exception {
        String categoryUrl=sisterUrl+"index.php/vod/type/id/"+tid+"/page/"+pg+".html";
        Document doc = Jsoup.parse(OkHttp.string(categoryUrl, getHeader()));
        List<Vod> list=ParseHtml(doc);
        return Result.string(Integer.parseInt(pg), Integer.parseInt(pg)+1,20,20,list);
    }

    @Override
    public String detailContent(List<String> ids) throws Exception {
        String detailUrl=sisterUrl+"index.php/vod/detail/id/"+ids.get(0)+".html";
        Document doc = Jsoup.parse(OkHttp.string(detailUrl, getHeader()));
        String pic = doc.select(".stui-content__thumb.fl-l .pic").attr("data-original");
        Elements detailInfo = doc.select(".stui-content__detail .fl-l");
        String title = detailInfo.select(".title").text();
//        String year = detailInfo.select(".data")
        String description = doc.select(".stui-content__desc.col-pd.clearfix").text();
        // 进行获取播放线路列表
        List<String> linkNameList=new ArrayList<>();
        List<String> linkList=new ArrayList<>();
        for (Element item : doc.select(".stui-pannel.clearfix")) {
            // 进行跳过第一个和最后一个
            String linkName = item.select(".title").text().trim();
            if (!linkName.contains("播")) continue;
            String url = item.select("li a").attr("href");
            linkNameList.add(linkName);
            linkList.add(url);
        }
        Vod vod = new Vod();
        vod.setVodId(ids.get(0));
        vod.setVodPic(pic);
        vod.setVodName(title);
        vod.setVodPlayFrom(TextUtils.join("$$$", linkNameList));
        vod.setVodPlayUrl(TextUtils.join("$$$", linkList));
        return Result.string(vod);
    }

    @Override
    public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
        String playUrl="";
        if(sisterUrl.endsWith("/")){
            playUrl=sisterUrl.substring(0,sisterUrl.lastIndexOf("/"))+"/"+id;
        }
        System.out.println(playUrl);
        String html_string = OkHttp.string(playUrl, getHeader());
        String regix="<script type=\"text/javascript\">var player_data=(.*)</script>";
        Pattern pattern = Pattern.compile(regix);
        String strObject = pattern.matcher(html_string).find() ? pattern.matcher(html_string).group(1) : "";
        System.out.println(strObject);
        return super.playerContent(flag, id, vipFlags);
    }

    private List<Vod> ParseHtml(Document doc) {
        Elements item_list = doc.select(".stui-vodlist__item");
        List<Vod> list = new ArrayList<>();
        for (Element item : item_list) {
            Elements info = item.select(".stui-vodlist__thumb");
            String url = info.attr("href");
            // /index.php/vod/detail/id/579718.html
            // 进行获取最后的id
            String id = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
            String title = info.attr("title");
            String pic = info.attr("data-original");
            String remark = item.select(".pic-text").text();
            System.out.println(remark);
            list.add(new Vod(id, title, pic, remark));
        }
        return list;
    }
}
